#include "signupwidget.h"
#include "ui_signupwidget.h"

const static QString pass="%%%Pass";

SignUpWidget::SignUpWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SignUpWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

}

SignUpWidget::~SignUpWidget()
{
    delete ui;
}


void SignUpWidget::signUpPass()
{
    ui->label_tips->setText("注册成功");
    PageManger::switchToWidget(PageManger::LOGINWIDGET);
}

void SignUpWidget::signUpError()
{
    ui->label_tips->setText("注册失败");
}



//以下为拖拽窗口代码
void SignUpWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(event->y()<50){
        m_bDrag = true;
        //获得鼠标的初始位置
        mouseStartPoint = event->globalPos();
        //获得窗口的初始位置
        windowTopLeftPoint = this->frameGeometry().topLeft();
        }
    }
}

void SignUpWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bDrag)
    {
        //获得鼠标移动的距离
        QPoint distance = event->globalPos() - mouseStartPoint;
        //改变窗口的位置
        this->move(windowTopLeftPoint + distance);
    }
}

void SignUpWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bDrag = false;
    }
}

void SignUpWidget::on_btn_close_clicked()
{
    this->close();
}

void SignUpWidget::on_btn_min_clicked()
{
    this->setWindowState(Qt::WindowMinimized);
}

void SignUpWidget::on_btn_back_clicked()
{
    PageManger::switchToWidget(PageManger::LOGINWIDGET);
}

void SignUpWidget::on_btn_signup_clicked()
{
    oc=ui->lineEdit_user->text();
    name=ui->lineEdit_name->text();
    QString password=ui->lineEdit_password->text();
    QString surepassword=ui->lineEdit_surepassword->text();
    QString regexPattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*(),.?\":{}|<>])[a-zA-Z\\d!@#$%^&*(),.?\":{}|<>]{8,}$";
    QRegularExpression expression(regexPattern);
    QRegularExpressionMatch match=expression.match(password);
    if(oc.isEmpty()||
       name.isEmpty()||
       avatar.isEmpty()||
       password.isEmpty()||
       surepassword.isEmpty())
    {
        ui->label_tips->setText("信息不可为空");
        return;
    }
    else if(password!=surepassword)
    {
        ui->label_tips->setText("两次密码不相同");
        return;

    }
    else if(!match.hasMatch())
    {
        ui->label_tips->setText("密码不符合规则");
        return;
    }
    else
    {
        ui->label_tips->setText("");
    }
    connect(JSHandler::getInstance(),&JSHandler::s_signUpPass,this,&SignUpWidget::signUpPass);
    connect(JSHandler::getInstance(),&JSHandler::s_signUpError,this,&SignUpWidget::signUpError);
    ThreadManger::getInstance()->sendSignup(oc,name,ui->lineEdit_password->text(),avatar);
}

void SignUpWidget::on_btn_chooseavatar_clicked()
{
    avatar=QFileDialog::getOpenFileName(this,"请选择你的头像","C:","Images(*.png *.jpg *.jpeg)");
    ui->label_avatar->setPixmap(avatar);
}

void SignUpWidget::on_btn_passwordvisible_clicked(bool checked)
{
    qDebug()<<"password check "<<checked;
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}

void SignUpWidget::on_btn_surepasswordvisible_clicked(bool checked)
{
    qDebug()<<"surepassword check "<<checked;

    if(checked)
    {
        ui->lineEdit_surepassword->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_surepassword->setEchoMode(QLineEdit::Password);
    }
}
